package com.test;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.bean.Dept;
import com.test.bean.Emp;
import com.test.bean.Employee;
import com.test.bean.SalgradeStatus;
import com.test.conf.SessionFactory;
import com.test.dao.DeptDao;
import com.test.dao.EmpMapper;
import com.test.dao.EmployeeDao;
import com.test.dao.SalgradeDao;
import com.test.mapper.SalgradeMapper;
import com.test.model.Salgrade;
import com.test.model.SalgradeExample;
import com.test.model.SalgradeExample.Criteria;

public class ApplicationMain {

	public static void main(String[] args) throws IOException {
		SessionFactory sessionFactory = new SessionFactory();
		SqlSessionFactory sqlSessionFactory = sessionFactory.sqlSessionFactory();
		SqlSession sqlSession = sqlSessionFactory.openSession();
		SqlSession sqlSession2 = sqlSessionFactory.openSession();
//		参数1: 建议加上命名空间唯一标示 参数2:Sql 需要的参数
		// Employee employee =
		// sqlSession.selectOne("com.test.dao.EmployeeDao.selectEmp", 1);
		// System.out.println(employee);
//		关闭session
		EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
		EmployeeDao mapper2 = sqlSession2.getMapper(EmployeeDao.class);
		Employee employeeById = mapper.getEmployeeById(7369);
		System.out.println(employeeById);
		sqlSession.close();

		Employee employeeById2 = mapper2.getEmployeeById(7369);
		System.out.println(employeeById2);
		System.out.println(employeeById == employeeById2);
		sqlSession2.close();
		// DeptDao deptDao = sqlSession.getMapper(DeptDao.class);
		// System.out.println(deptDao.getAllDept());
//		StudentDao studentMapper = sqlSession.getMapper(StudentDao.class);
//		System.out.println(studentMapper.getAllStudents());

	}

	@Test
	public void test1() {
		SessionFactory sessionFactory = new SessionFactory();
		SqlSession sqlSession = null;
		try {
			sqlSession = sessionFactory.createSqlSession();
			EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
			Employee employee = new Employee();
			employee.setEmail(null);
			employee.setGender("0");
			employee.setLastName("张三");
			// employee.setId(1);
			int insertEmployeed = mapper.insertEmployeed(employee);
			// int updateEmployeed = mapper.updateEmployeed(employee);
			List<Employee> findAllEmployeed = mapper.findAllEmployeed();
//			int deleteEmployeed = mapper.deleteEmployeed(employee);
			sqlSession.commit();
			// System.out.println("insert count==="+insertEmployeed+"id==="+employee);
			// System.out.println("update count==="+updateEmployeed);
//			System.out.println("delete count==="+deleteEmployeed);
			// System.out.println("findAll count==="+findAllEmployeed.size());
			Employee employeeByIdAndLastName = mapper.getEmployeeByIdAndLastNameByParam("tb1_employee", "8", "panyong");
			System.out.println(employeeByIdAndLastName);
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("id", "10");
			map.put("lastName", "panyong");
			Employee employeeByIdAndLastMap = mapper.getEmployeeByIdAndLastMap(map);
			// System.out.println(employeeByIdAndLastMap);
			Set<Employee> employeeByLastName = mapper.getEmployeeByLastName("panyong");
			System.out.println("Set=========" + employeeByLastName + "=========size" + employeeByLastName.size());
			Map<Integer, Employee> geEmployeetMap = mapper.geEmployeetMap();
			System.out.println("map=======" + geEmployeetMap);
			SortedMap<Date, Employee> geEmploySortedMap = mapper.geEmploySortedMap();
			System.out.println("SortedMap====" + geEmploySortedMap);

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}

	}

	@Test
	public void test01() {
		SessionFactory sessionFactory = new SessionFactory();
		SqlSession createSqlSession = null;
		try {
			createSqlSession = sessionFactory.createSqlSession();
			EmpMapper mapper = createSqlSession.getMapper(EmpMapper.class);
//			Emp emp = new Emp();
//			emp.setEmpno(7788);
//			emp.setEname("HUYONG");

			// List<Emp> discriminator = mapper.getEmpDiscriminator();
			// List<Emp> empByNgolIf = mapper.getEmpByNgolIf(null, "SCOTT");
			// List<Emp> empByNgoltrim = mapper.getEmpByNgoltrim(null, "SCOTT","ANALYST");
			// System.out.println(empByNgoltrim);
			// List<Emp> empByNgolChoose = mapper.getEmpByNgolChoose(null,null);
			// System.out.println(empByNgolChoose);
			//// List<Emp> emp = mapper.getEmp();
			// System.out.println(emp);
			// Emp empAndDept = mapper.getEmpByIdStep(7782);
			// System.out.println(empAndDept.getEname());
			// System.out.println(empAndDept.getDept());
//			DeptDao deptDao = createSqlSession.getMapper(DeptDao.class);
//			System.out.println(deptDao.getDeptById(10));
//			int updateEmpBySet = mapper.updateEmpBySet(emp);
//			System.out.println(updateEmpBySet);
			// List<Integer> ids=new ArrayList<Integer>();
//			ids.add(7788);
//			ids.add(7839);
//			ids.add(7844);
//			List<Emp> emps=mapper.getEmpByIn(ids);
//			System.out.println(emps);
			List<Emp> emps = new ArrayList<Emp>();
			Emp emp1 = new Emp();
			emp1.setEmpno(8881);
			emp1.setEname("张三");
			Emp emp2 = new Emp();
			emp2.setEmpno(9991);
			emp2.setEname("李四");
			Dept dept = new Dept();
			dept.setDeptNo(10);
			emp1.setDept(dept);
			emp2.setDept(dept);
			emps.add(emp1);
			emps.add(emp2);
			int saveEmps = mapper.saveOracle1Emps(emps);
			System.out.println(saveEmps);
			createSqlSession.commit();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (createSqlSession != null) {
				createSqlSession.close();
			}
		}

	}

	@Test
	public void test02() {
		SessionFactory sessionFactory = new SessionFactory();
		SqlSession createSqlSession = null;
		try {
			createSqlSession = sessionFactory.createSqlSession();
			DeptDao mapper = createSqlSession.getMapper(DeptDao.class);
			Dept deptByIdPlus = mapper.getDeptByIdStepMParam(10);// 这里是有延迟加载
			System.out.println(deptByIdPlus.getDname());
			// System.out.println(deptByIdPlus.getEmps());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (createSqlSession != null) {
				createSqlSession.close();
			}
		}

	}

	@Test
	public void test03() {
		SessionFactory factory = new SessionFactory();
		SqlSession createSqlSession = null;
		try {
			createSqlSession = factory.createSqlSession();
			EmpMapper mapper = createSqlSession.getMapper(EmpMapper.class);
			Emp emp = new Emp();
			emp.setEname("李");
			mapper.getEmpsTestInnerParam(emp);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (createSqlSession != null) {
				createSqlSession.close();
			}
		}
	}

	@Test
	public void testMbg() {
		try {
			List<String> warnings = new ArrayList<String>();
			boolean overwrite = true;
			File configFile = new File("generatorConfig.xml");
			ConfigurationParser cp = new ConfigurationParser(warnings);
			Configuration config = cp.parseConfiguration(configFile);
			DefaultShellCallback callback = new DefaultShellCallback(overwrite);
			MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
			myBatisGenerator.generate(null);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Test
	public void testMbgCURD() {
		SessionFactory factory = new SessionFactory();
		SqlSession createSqlSession = null;
		try {
			createSqlSession = factory.createSqlSession();
			SalgradeMapper mapper = createSqlSession.getMapper(SalgradeMapper.class);
			SalgradeExample example = new SalgradeExample();
			Criteria createCriteria = example.createCriteria();
			Short sh = 1;
			createCriteria.andGradeEqualTo(sh);
			// example 用来封装查询条件的 //查询所有
			List<Salgrade> selectByExample = mapper.selectByExample(example);
			System.out.println(selectByExample);
			SalgradeExample delete = new SalgradeExample();
			int deleteByExample = mapper.deleteByExample(delete);
			System.out.println(deleteByExample);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (createSqlSession != null) {
				createSqlSession.close();
			}
		}
	}

	@Test
	public void testPageHalper() {
		SessionFactory factory = new SessionFactory();
		SqlSession createSqlSession = null;
		try {
			createSqlSession = factory.createSqlSession();
			SalgradeMapper mapper = createSqlSession.getMapper(SalgradeMapper.class);
			Page<Salgrade> startPage = PageHelper.startPage(1, 5);
			// example 用来封装查询条件的 //查询所有
			List<Salgrade> selectByExample = mapper.selectByExample(null);
			System.out.println(selectByExample);
			System.out.println(startPage);
			// 参数5表示连续显示多少页
			PageInfo<Salgrade> pageInfo = new PageInfo<Salgrade>(selectByExample, 5);
			System.out.println(pageInfo);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (createSqlSession != null) {
				createSqlSession.close();
			}
		}
	}

	/**
	 * 测试mybatis 的批量保存 huyong
	 * 
	 * @return Aug 10, 2019
	 */
	@Test
	public void SaveByBatch() {
		SessionFactory factory = new SessionFactory();
		SqlSession openSession = null;
		try {
			SqlSessionFactory sqlSessionFactory = factory.sqlSessionFactory();
			// 创建一个并发操作的对象
			openSession = sqlSessionFactory.openSession();
			// 批量操作 12231
			// 非批量 708192 好费事啊
			SalgradeDao mapper = openSession.getMapper(SalgradeDao.class);
			long startTime = System.currentTimeMillis();
			for (int i = 0; i < 10; i++) {
				Salgrade salgrade = new Salgrade();
				Short num = 111;
				salgrade.setGrade(num);
				salgrade.setHisal(num);
				salgrade.setLosal(num);
				salgrade.setStatus(SalgradeStatus.LOGIN);
				int save = mapper.save(salgrade);
				System.out.println(save);
			}
			long endTime = System.currentTimeMillis();

			System.out.println(endTime - startTime);
			openSession.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (openSession != null) {
				openSession.close();
			}
		}

	}

	/**
	 * oracle 存储过程 进行分页 huyong Aug 11, 2019
	 */
	@Test
	public void testProcedure() {
		SessionFactory factory = new SessionFactory();
		SqlSession openSession = null;
		try {
			SqlSessionFactory sqlSessionFactory = factory.sqlSessionFactory();
			// 创建一个并发操作的对象
			openSession = sqlSessionFactory.openSession();
			// 批量操作 12231
			// 非批量 708192 好费事啊
			SalgradeDao mapper = openSession.getMapper(SalgradeDao.class);
			long startTime = System.currentTimeMillis();
			com.test.bean.Page page=new com.test.bean.Page();
			page.setStart(1);
			page.setEnd(5);
			mapper.getPageByProcedure(page);
			System.out.println(page.getSalgrades());
			List<Salgrade> salgrades = page.getSalgrades();
			for (Salgrade salgrade : salgrades) {
				SalgradeStatus status = salgrade.getStatus();
				System.out.println(status);
			}
			long endTime = System.currentTimeMillis();

			System.out.println(endTime - startTime);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (openSession != null) {
				openSession.close();
			}
		}

	}
}
